﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using V5_PhoneLibs;
using V5_Book.Model;

namespace V5_Book.View.GetResource {
    public partial class Demo : PhoneApplicationPage {
        int requestTime = 0;
        public Demo() {
            InitializeComponent();
            this.btnSend.Click += new RoutedEventHandler(btnSend_Click);
        }
        delegate void DelegateTest(SocketAsyncEventArgs ea);
        SocketClient client;

        void btnSend_Click(object sender, RoutedEventArgs e) {
            client = new SocketClient();
            StringBuilder sbText = new StringBuilder();
            string fileName = "不灭之旅" + DateTime.Now.ToString("yyyyMMddHHmmss");
            int fileLength = 0;
            client.Connect("192.168.3.185", 4530);
            client.Osd = delegate(string fileTitle, string content) {
                string s = content;

                List<ModelBookInfo> listBook = BookHelper.Instance.ListBookInfo();

                BookHelper.Instance.AddBookInfo(fileName + ".txt", s);

                listBook.Add(new ModelBookInfo() {
                    BookName = fileName,
                    BookIndex = 1,
                    LastTime = DateTime.Now,
                    CreateTime = DateTime.Now
                });

                BookHelper.Instance.SaveListBookInfo(listBook, null);
                /*
                Dispatcher.BeginInvoke(new DelegateTest(delegate(SocketAsyncEventArgs ea) {
                    var response = Encoding.UTF8.GetString(ea.Buffer, ea.Offset, ea.BytesTransferred);
                    response = response.Trim('\0');
                    sbText.Append(response);
                    //fileLength += ea.Buffer.Length;
                    //this.tbSend.Text = fileLength + "||" + sbText.ToString();
                    if (requestTime == 0) {
                        string[] fileArr = response.Split('|');
                        fileName = fileArr[1];
                        fileLength = int.Parse(fileArr[0]);
                    }
                    else if (sbText.Length < fileLength) {
                        sbText.Append(response);
                        if (sbText.Length >= fileLength) {
                            BookHelper.Instance.AddBookInfo(fileName + ".txt", sbText.ToString());
                        }
                    }
                    else {
                        BookHelper.Instance.AddBookInfo(fileName + ".txt", sbText.ToString());
                    }
                    requestTime++;
                }), ea1);
                */
            };
            var response1 = client.Receive();
            //Thread th = new Thread(Run);
            //th.Start();
        }

    }
}